<?php
/**
 * Created by PhpStorm.
 * User: xeng
 * Date: 2018/1/31
 * Time: 19:26
 */

/*
 * 金币管理 类
 */
namespace Home\Controller;
use Think\Controller;
use Think\Model;

class GoldController extends AuthController {
    /*
     * 金币记录
     * record
     */
    public function record(){
        //拿到条件 分页查询
        $condi=$_GET;
        $condi=array_merge($condi,$_POST);
        $p=$condi['p']?$condi['p']:1;         //第几页
        $listRows=30 ;                   //每页记录数
        $firstRow =($p-1)*$listRows;
        
        $where="";
        if($condi['start_time']){
            $where =" where datetime>='{$condi['start_time']}'";
        }
        if($condi['end_time']){
            if($where){
                $where .=" and datetime<='{$condi['end_time']}'";
            }else{
                $where =" where datetime<='{$condi['end_time']}'";
            }
        }
        if($condi['card_no']){
            if($where){
                $where .=" and gameid='{$condi['card_no']}'";
            }else{
                $where =" where gameid='{$condi['card_no']}'";
            }
        }
        
        $count = $this->mysql1("SELECT count(*) FROM data_bankgoldadd $where");
        $show = $this->page_show($count,10,$p);  

        $lists = $this->mysql1("
        SELECT * FROM data_bankgoldadd $where  ORDER BY id DESC LIMIT $firstRow,$listRows
        ");
        $this->assign("p",$condi['p']);
        $this->assign("count",$count);
        $this->assign("condi",$condi);
        $this->assign('page_show',$show);
        $this->assign('lists',$lists);
        $this->display();
    }

    /*
     * 生成金币
     *
     * 修改sqlsrv GameScoreInfo 表 的bankgold
     * 存入操作日志 sqlsrv web_BankgoldAdd
     *              mysql  data_bankgoldadd
     */
    public function create(){
        $goldadd =trim($_GET['goldadd']);
        $gameid = trim($_GET['gameid']);
        if($goldadd !='' && $gameid!=''){
            $gameid = "'".$gameid."'";
            $lists = $this->sqlsrv1("
SELECT a.accountid,a.account,a.nikename,a.gameid,b.bankgold FROM UserAccount AS a,GameScoreInfo AS b WHERE a.accountid=b.accountid AND gameid=$gameid
            ");
            if(!$lists){
                $this->err404("该会员游戏ID可能不存在<br>请检测添加条件！");
            }
            $data=reset($lists);
            $accountid = $data['accountid'];
            $goldnum =$data['bankgold']+$goldadd;
            if( $accountid && $goldnum>=$goldadd){
                if(!isset($data['bankgold'])){
                    $this->err404("写入操作日志停止，添加金币数错误");
                }
                $accountid = "'".$accountid."'";
                $addresult = $this->sqlsrv1("
UPDATE GameScoreInfo SET bankgold =$goldnum where accountid=$accountid
                ");
                if(!$addresult){
                    $this->err404("金币生成成功<br>写入操作记录失败！");
                }
               
                if($addresult){
                    $account ="'".$lists[0]['account']."'";
                    $nikename ="'".$lists[0]['nikename']."'";
                    $datetime = "'".date('Y-m-d H:i:s',time())."'";
                    $operatorid =  "'".$_SESSION['ltq_game_admin_user']["id"]."'";
                    $operator = "'".$_SESSION['ltq_game_admin_user']["name"]."'";
                   
                    $insertMysqlResult = $this->mysql1("
INSERT INTO data_bankgoldadd VALUES ('',$datetime,$operatorid,$operator,$account,$nikename,$gameid,$goldadd)
                    ");
                  

                    if($insertMysqlResult){
                        $this->redirect('Gold/searchadd', array('datetime' =>$datetime,'operator'=>$operator) );
                    }else{
                        $this->err404("写入日志失败","错误");
                    }
                }
            }


        }else{
            $this->err404("参数不正确！");
        }
    }
    /*
     * 添加金币之后查询添加结果
     * search create
     */
    public function searchadd(){
        $datetime =$_REQUEST['datetime'];
        $operator =$_REQUEST['operator'];
        if($datetime=="''" || $operator=="''"){
            $this->err404("写入数据后，查询的参数错误！！");
        }
        $lists=$this->mysql1("
select * from data_bankgoldadd where datetime=$datetime  and operator=$operator
                        ");
        $this->assign('lists',$lists);
        $this->display('searchone');
    }

    /*
     * 条件搜索
     */
    public function search(){
        echo "search";
    }
}
